<template>
  <el-card>
    <el-table :data="skuArr" :border="true">
      <el-table-column type="index" label="序号" width="80" />
      <el-table-column prop="skuName" label="名称" width="150" />
      <el-table-column prop="skuDesc" label="描述" width="150" />
      <el-table-column label="图片" width="150">
        <template #default="{ row }">
          <img :src="row.skuDefaultImg" style="wdith: 100px; height: 100px" />
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量" width="150" />
      <el-table-column prop="price" label="价格" width="150" />
      <el-table-column label="操作" width="260" fixed="right">
        <template #default="{ row }">
          <el-button
            type="primary"
            size="small"
            :icon="row.isSale === 1 ? 'Bottom' : 'Top'"
            @click="updateSale(row)"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="Edit"
            @click="updateSku"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="InfoFilled"
            @click="findSku(row)"
          ></el-button>

          <el-popconfirm
            :title="`确认删除${row.skuName}`"
            width="200px"
            @confirm="deleteSku(row)"
          >
            <template #reference>
              <el-button type="danger" size="small" icon="Delete"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 40]"
      :background="true"
      layout=" prev, pager, next, jumper, ->, total, sizes"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="getHasSku"
    />

    <el-drawer v-model="drawer" direction="rtl">
      <template #header>
        <div>查看详情</div>
      </template>
      <el-row>
        <el-col :span="6">名称</el-col>
        <el-col :span="18">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="6">描述</el-col>
        <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="6">价格</el-col>
        <el-col :span="18">{{ skuInfo.price }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="6">平台属性</el-col>
        <el-col :span="18">
          <el-tag v-for="item in skuInfo.skuAttrValueList" :key="item.id">
            {{ item.valueName }}
          </el-tag>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">销售属性</el-col>
        <el-col :span="18">
          <el-tag v-for="item in skuInfo.skuSaleAttrValueList" :key="item.id">
            {{ item.saleAttrValueName }}
          </el-tag>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">商品图片</el-col>
        <el-col :span="18">
          <el-carousel :interval="4000" type="card" height="200px">
            <el-carousel-item
              v-for="item in skuInfo.skuImageList"
              :key="item.id"
            >
              <img :src="item.imgUrl" style="width: 100px; height: 100px" />
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </el-card>
</template>

<script setup lang="ts">
import {
  reqSkuList,
  reqSaleSku,
  reqCancelSale,
  reqSkuInfo,
  reqRemoveSku
} from '@/api/product/sku/index'
import { ElMessage } from 'element-plus'
import type {
  SkuData,
  SkuResponseData,
  SkuInfoData
} from '@/api/product/sku/type'

import { ref, onMounted } from 'vue'

const pageNo = ref<number>(1)
const pageSize = ref<number>(40)
const total = ref<number>(0)

onMounted(() => {
  getHasSku()
})
const skuArr = ref<SkuData[]>([])
const getHasSku = async (pager = 1) => {
  pageNo.value = pager
  const result: SkuResponseData = await reqSkuList(pageNo.value, pageSize.value)
  skuArr.value = result.data.records
  total.value = result.data.total
}
const handleSizeChange = () => {
  getHasSku(1)
}
const updateSale = async (row: SkuData) => {
  if (row.isSale === 1) {
    await reqCancelSale(row.id as number)
    ElMessage({
      type: 'success',
      message: '下架成功'
    })
  } else {
    await reqSaleSku(row.id as number)
    ElMessage({
      type: 'success',
      message: '上架成功'
    })
  }

  getHasSku(pageNo.value)
}
const updateSku = () => {
  ElMessage({
    type: 'success',
    message: '努力更新中'
  })
}

const drawer = ref<boolean>(false)
const skuInfo = ref<SkuData>({})
const findSku = async (row: SkuData) => {
  drawer.value = true

  await reqSkuInfo(row.id as number)

  skuInfo.value = row
  console.log(row)
}
const deleteSku = async (row: SkuData) => {
  await reqRemoveSku(row.id as number)
  ElMessage({
    type: 'success',
    message: '删除成功'
  })

  getHasSku(skuArr.value.length > 1 ? pageNo.value : pageNo.value - 1)
}
</script>

<style lang="scss">
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
